<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #box1{
            width: 100px;
            height: 100px;
            background-color: #bafa;
            position: absolute;

        }
    </style>
    <script>
        window.onload=function () {
            var box1=document.getElementById("box1");
            var speed=10;
            var dir=0;
            setInterval(function () {

                switch (dir) {
                    case 37:
                        box1.style.left=box1.offsetLeft-speed+"px";
                        break;
                    case 38:
                        box1.style.top=box1.offsetTop-speed+"px";
                        break;
                    case 39:
                        box1.style.left=box1.offsetLeft+speed+"px";
                        break;
                    case 40:
                        box1.style.top=box1.offsetTop+speed+"px";
                }
            },30);
            document.onkeydown=function (event) {
                event=event||window.event;
                //左 37 上38 右39 下40
                if (event.ctrlKey){
                    speed=50;
                }else {
                    speed=10;
                }
                dir=event.keyCode;

            };
            document.onkeyup=function () {
                dir=0;
            }
        }
    </script>
</head>
<body>
<div id="box1"></div>
</body>
</html>